@ECHO OFF
ECHO.
ECHO.Usage: Command.InstallAndRegister.cmd [/u][/debug]
ECHO.

set CompanyName=Microsoft
set ProductName=McePicasa
set AssemblyName=McePicasa
set RegistrationFilename=McePicasa.xml
set ImageName=McePicasa.logo.png
set ImageName2=Flickr.logo.png
set ImageName3=Picasa.logo.png
set ProgramFilesPathForGac=%ProgramFiles%

REM Determine whether we are on an 32 or 64 bit machine
if "%PROCESSOR_ARCHITECTURE%"=="x86" if "%PROCESSOR_ARCHITEW6432%"=="" goto x86

ECHO.On an x64 machine
set ProgramFilesPath=%ProgramFiles(x86)%
set Netfx20Path=%windir%\Microsoft.NET\Framework64\v2.0.50727
ECHO.

goto unregister

:x86

	ECHO.On an x86 machine
	set ProgramFilesPath=%ProgramFiles%
	set Netfx20Path=%windir%\Microsoft.NET\Framework\v2.0.50727
	ECHO.

:unregister

if exist "%ProgramFilesPathForGac%\Microsoft.NET\SDK\v2.0\bin\gacutil.exe" (
  set gac="%ProgramFilesPathForGac%\Microsoft.NET\SDK\v2.0\bin\gacutil.exe"
) ELSE (
  if exist "%ProgramFilesPathForGac%\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe" (
    set gac="%ProgramFilesPathForGac%\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe"
  ) else (
    set gac="%ProgramFilesPathForGac%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe"
  )
)
echo gac = %gac%

	ECHO.Unregister and delete previously installed files (which may fail if nothing is registered)
	ECHO.

	ECHO.Unregister the application entry points
	%windir%\ehome\RegisterMCEApp.exe /allusers "%ProgramFilesPath%\%CompanyName%\%ProductName%\%RegistrationFilename%" /u
	ECHO.

	ECHO.Remove the DLL from the Global Assembly cache
	%gac% /u "%AssemblyName%"
	ECHO.

	ECHO.Delete the folders containing the DLLs and supporting files (silent if successful)
	rd /s /q "%ProgramFilesPath%\%CompanyName%\%ProductName%"
	ECHO.

	ECHO.Delete the InstallLocation registry value
	reg delete "HKLM\Software\%CompanyName%\%ProductName%" /v InstallLocation /f
	ECHO.
	
	REM Exit out if the /u uninstall argument is provided, leaving no trace of the program files.
	if "%1"=="/u" goto exit
	
:releasetype

	REM evaluate the second argument
	if "%1"=="/debug" goto debug
	
	ECHO.Using the release version of the binaries
	set ReleaseType=Release
	ECHO.
	
	goto checkbin
	
:debug

	ECHO.Using the Debug version of the binaries
	set ReleaseType=Debug
	ECHO.
	
:checkbin

    if exist ".\bin\%ReleaseType%\%AssemblyName%.dll" goto register
    ECHO.Cannot find %ReleaseType% binaries.
    ECHO.Build solution as %ReleaseType% and run script again. 
    goto exit

:register

	REM Copying and registering assemblies

	ECHO.Create the path for the binaries and supporting files (silent if successful)
	md "%ProgramFilesPath%\%CompanyName%\%ProductName%"
	ECHO.
	
	ECHO.Copy the assemblies to Program Files
	copy /y ".\bin\%ReleaseType%\%AssemblyName%.dll" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	
	rem ECHO.Copy the Download Manager Server to Program Files
	rem copy /y ".\DownloadManagerService\bin\%ReleaseType%\DownloadManagerService.exe" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	rem copy /y ".\DownloadManagerService\bin\%ReleaseType%\DownloadManagerService.exe.config" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	ECHO.
	
	ECHO.Copy the logos to program files
	copy /y ".\%ImageName%" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	copy /y ".\%ImageName2%" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	copy /y ".\%ImageName3%" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	ECHO.
	
	ECHO.Copy the registration XML to program files
	copy /y ".\%RegistrationFilename%" "%ProgramFilesPath%\%CompanyName%\%ProductName%\"
	ECHO.

	ECHO.Register the DLL with the global assembly cache
	%gac% /if "%ProgramFilesPath%\%CompanyName%\%ProductName%\%AssemblyName%.dll"
	ECHO.

	ECHO.Register the application with Windows Media Center
	%windir%\ehome\RegisterMCEApp.exe /allusers "%ProgramFilesPath%\%CompanyName%\%ProductName%\%RegistrationFilename%"
	ECHO.

	ECHO.Add the InstallLocation registry value
	reg add "HKLM\Software\%CompanyName%\%ProductName%" /v InstallLocation /t REG_SZ /d "%ProgramFilesPath%\%CompanyName%\%ProductName%\\" /f
	ECHO.
	
:exit